angular.module('meindexcontrollers', [])

  .controller('MeIndexCtrl', function ($scope,$state,$rootScope,Toast,Loading,Storage,MeIndex,ENV) {


    var memberId;
    $scope.ENV = ENV;
    $scope.isIdentity = true;
    var identifyGrade = 2000;
    $scope.isSendIdentityMsg = false;

    $scope.meInfo = {
      member_attention_count : 0,
      member_collection_count : 0,
      member_appointment_count : 0,
      member_home_count : 0,
      member_identify_state : "",
      member_name:"",
      member_img_url:"",
      mr_img_url:""
    };

    $scope.meIdentifyMsg = {
      identityMsg:"",
      identityState:""
    };

    //进入我的首页，进行数据加载
    $scope.$on('$ionicView.beforeEnter', function () {
      $rootScope.showWineGlasses = false;
      memberId = Storage.get("member").member_id;
      MeIndex.getMeHomeInfo(memberId);
    });

    //获取返回的json对象，赋值给页面
    $scope.$on('Me.meHomeInfoGot', function () {
      var obj = MeIndex.getMeHomeInfoJson();
      $scope.meInfo.member_attention_count = obj.member_attention_count;
      $scope.meInfo.member_collection_count = obj.member_collection_count;
      $scope.meInfo.member_appointment_count = obj.member_appointment_count;
      $scope.meInfo.member_home_count = obj.member_home_count;
      $scope.meInfo.member_identify_state = obj.member_identify_state;
      $scope.meInfo.member_name = obj.member_name;
      $scope.meInfo.member_img_url = obj.member_img_url;
      $scope.meInfo.mr_img_url = obj.mr_img_url;
      $scope.meInfo.member_point_count_history = obj.member_point_count_history;
      $scope.meInfo.member_identify_state = obj.member_identify_state;

      //分数达到并且认证状态为未审核或者审核失败
      if(obj.member_point_count_history > identifyGrade && (obj.member_identify_state == '未审核' || obj.member_identify_state == '审核失败')){
        $scope.isIdentity = false;
      }
    });

    $scope.gotoMyData = function() {
      $state.go('my-data');
    }


    //发送认证消息给后台
    $scope.sendIdentifyMsg = function() {
      MeIndex.sendIdentifyMsgById(memberId);
    }

    //获取返回的json对象，赋值给页面
    $scope.$on('Me.identiyMsgGot', function () {
      var obj = MeIndex.getIdentityMsgJson();
      Toast.showMessage(obj.identityMsg);
      $scope.isIdentity = true;

    });

    $scope.gotoMyIntegratPage = function() {
      $state.go('my-integrat');
    }

    //跳转到积分商城
    $scope.gotoIntegratListPage = function () {
      $state.go('integrat-list');
    }


  });
